const app = getApp();
let redg = -1080;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eqSn: "",
    deviceInfo: {},
    deviceStatus: [],
    flag: false,
    animationData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      eq_sn,
    } = options;
    this.setData({
      eqSn: eq_sn,
    })
    this.getDeviceInfo();
    this.getDeviceStatus();
    this.getUserData();
  },

  //打开地图
  navigateAddress(e) {
    let {
      longitude,
      latitude
    } = e.currentTarget.dataset;
    longitude = Number(longitude);
    latitude = Number(latitude);
    if (latitude == 0 || latitude == '' || longitude == 0 || longitude == '') {
      app.showToast("位置信息不全,请换个园区重试", "none", 2000, function () { });
      return false;
    }
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18
    })
  },
  getUserData() {
    app.ajax({
      url: 'Manage/User/getManageInfo',
      method: "POST",
      data: {},
      success: res => {
        if (res.data.code == 200) {
          this.setData({
            is_auth: res.data.data.is_auth
          })
        } else {
          app.showToast("获取用户信息失败");
        }
      }
    })
  },

  //设备数据
  getDeviceInfo() {
    app.ajax({
      url: 'Manage/Equipment/deviceInfo',
      data: {
        sn: this.data.eqSn,
      },
      success: res => {
        if (res.data.code == 1000) {
          this.setData({
            deviceInfo: res.data.data
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取设备状态
  getDeviceStatus() {
    app.ajax({
      url: 'Manage/Equipment/device_status',
      data: {
        sn: this.data.eqSn,
      },
      success: (res) => {
        if (res.data.code == 1000) {
          let flag = false;
          if (res.data.data.equipment_switchgear == 2) {
            flag = true;
          }
          this.setData({
            deviceStatus: res.data.data,
            flag: flag
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },


  //点击调用动画
  upData() {
    const animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      success: function (res) {
        console.log("动画结束")
      }
    })
    if (redg) {
      redg -= 1080;
    }
    this.animation = animation
    animation.rotate(redg).step()
    this.setData({
      animationData: animation.export()
    })

    this.getDeviceStatus();
  },

  //开关处理
  switchChange: function (e) {
    var that = this;
    that.setData({
      flag: !that.data.flag
    })
    let val = 1;
    if (that.data.flag == true) {
      val = 2;
    }
    app.ajax({
      url: 'Manage/Equipment/switchgear',
      data: {
        sn: that.data.eqSn,
        val: val,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message);
          setTimeout(() => {
            that.getDeviceStatus();
          }, 3000);
        } else {
          app.showToast(res.data.message);
          that.setData({
            ['deviceStatus.equipment_switchgear']: val == 1 ? 2 : 1
          })
        }
      }
    })
  },


  //水机参数
  waterMachineParam() {
    wx.navigateTo({
      url: '../waterMachineParam/waterMachineParam?eq_sn=' + this.data.eqSn,
    })
  },

  //滤芯更换记录
  filterCoreRecord: function () {
    wx.navigateTo({
      url: '../filterCoreRecord/filterCoreRecord?eq_sn=' + this.data.eqSn,
    })
  },




  //消费记录
  consumeList: function () {
    wx.navigateTo({
      url: '../consumeList/consumeList?eq_sn=' + this.data.eqSn,
    })
  },


  //跳转打水二维码
  navDeviceQrcode() {
    wx.navigateTo({
      url: '../deviceQrcode/deviceQrcode?eq_sn=' + this.data.eqSn,
    })
  },

  //跳转售水参数修改
  navSellingWater() {
    wx.navigateTo({
      url: '../parameter/sellingWater?eq_sn=' + this.data.eqSn,
    })
  },
  //跳转制水参数修改
  navMakingWater() {
    wx.navigateTo({
      url: '../parameter/makingWater?eq_sn=' + this.data.eqSn,
    })
  },
  //跳转控制参数修改
  navControlParameter() {
    wx.navigateTo({
      url: '../parameter/controlParameter?eq_sn=' + this.data.eqSn,
    })
  },
  //跳转系统参数修改
  navSystemParameter() {
    wx.navigateTo({
      url: '../parameter/systemParameter?eq_sn=' + this.data.eqSn,
    })
  },

})